package nautilus.lab.activity;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import nautilus.lab.R;
import nautilus.lab.renderer.SnappingHScrollView;
import nautilus.service.DataReceiver;
import nautilus.service.IMessageHandler;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SnappingHScrollActivity extends Activity implements IMessageHandler{
	private static final String tag = "SnappingHScrollActivity";
	private SnappingHScrollView hview;
	private TextView txtCurrentItem, txtScrollX;
	private Button btnAddMoreItemBegin, btnAddMoreItemEnd, btnRemoveItemBegin, btnRemoveItemEnd;
	private DataReceiver dataReceiver;
	private LocalBroadcastManager localBroadcastManager = null;//LocalBroadcastManager.getInstance(getContext());
	
	public void onCreate(Bundle savedState){
		super.onCreate(savedState);
		setContentView(R.layout.snapping_slide);
		
		hview = (SnappingHScrollView)findViewById(R.id.snapping_slide_hscroll_view);
		txtCurrentItem = (TextView)findViewById(R.id.current_item);
		txtScrollX = (TextView)findViewById(R.id.get_scroll);
		
		btnAddMoreItemBegin = (Button)findViewById(R.id.snapping_slide_btn_add_more_item_begin);
		btnAddMoreItemBegin.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				hview.addItemToBegin();
			}
			
		});
		
		btnAddMoreItemEnd = (Button)findViewById(R.id.snapping_slide_btn_add_more_item_end);
		btnAddMoreItemEnd.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				hview.addItemToEnd();
			}
			
		});
		
		btnRemoveItemBegin = (Button)findViewById(R.id.snapping_slide_btn_remove_item_begin);
		btnRemoveItemBegin.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				hview.removeItemBegin();
			}
			
		});
		
		btnRemoveItemEnd = (Button)findViewById(R.id.snapping_slide_btn_remove_item_end);
		btnRemoveItemEnd.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				hview.removeItemEnd();
			}
			
		});
		
		String externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
		File targetDirector = new File(externalStoragePath);
		//File targetDirector = new File("mnt/ext_card/DCIM/100ANDRO/");
		//Log.i("SnappingHScrollActivity", externalStoragePath);
		File[] files = targetDirector.listFiles();
		List<Bitmap> bitmaps = new ArrayList<Bitmap>();
        for (File file : files){
        	if(file.getName().contains("jpg"))
        		bitmaps.add(MyGalleryActivity.decodeSampledBitmapFromUri(file.getAbsolutePath(), 100, 120));
        }
		hview.setItemSize(180, 220);
		hview.setPadding(1, 1);
		hview.setMargin(10, 10);
		hview.setFeatureItems(bitmaps);
		
		dataReceiver = new DataReceiver();
		dataReceiver.addHandler(this);
		localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
	}
	
	@Override
	protected void onResume() {
		super.onResume();		
		localBroadcastManager.registerReceiver(dataReceiver, new IntentFilter("NAUTILUS_DATA_COMMUNICATION"));
	};
	
	@Override
	protected void onPause(){
		localBroadcastManager.unregisterReceiver(dataReceiver);
		super.onPause();
	}

	@Override
	public void onDataReceived(Intent intent) {
		int item = intent.getIntExtra("ITEM", -1);
		float from = intent.getFloatExtra("FROM", -1);
		float to = intent.getFloatExtra("TO", -1);
		int scrollTo = intent.getIntExtra("SCROLLTO", -1);
		int mw = intent.getIntExtra("MEASUREDWIDTH", -1);
		String str = String.format("ScrollX: %f -> %f; Scrolled to: %d; measured width: %d", from, to, scrollTo,  mw);
		txtCurrentItem.setText(item+"");
		txtScrollX.setText(str);
	}

}
